package com.zygame.sdk.utils.phone;

import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import android.os.Environment;
import android.text.TextUtils;

/**
 * 文件操作类
 */
public class FileUtil {

	/**
	 * 创建文件的模式，已经存在的文件要覆盖
	 */
	public final static int MODE_COVER = 1;

	/**
	 * 创建文件的模式，文件已经存在则不做其它事
	 */
	public final static int MODE_UNCOVER = 0;
	
	private final static String TAG = "FileUtil";

	/*
	 * 将raw里的文件copy到sd卡下
	 */
//	public void copyResToSdcard(String name,Context context) {// name为sd卡下制定的路径
//		File file = new File(name);
//		if(file.exists()){
//			file.delete();
//		}else{
//			file.mkdirs();
//		}
//		Field[] raw = R.raw.class.getFields();
//		for (Field r : raw) {
//			try {
//				int id = context.getResources().getIdentifier(r.getName(), "raw",context.getPackageName());
//				if (!r.getName().equals("allapps")) {
//					if(r.getName().equals("address") || r.getName().equals("config.json") || r.getName().equals("terms.json")){
//						continue;
//					}
//					String path = name + "/" + r.getName() + ".png";
//					File iconFile = new File(path);   
//					if(!iconFile.exists()){
//						iconFile.createNewFile();
//					}
//					
//					BufferedOutputStream bufEcrivain = new BufferedOutputStream((new FileOutputStream(iconFile)));
//					BufferedInputStream VideoReader = new BufferedInputStream(context.getResources().openRawResource(id));
//					byte[] buff = new byte[20 * 1024];
//					int len;
//					while ((len = VideoReader.read(buff)) > 0) {
//						bufEcrivain.write(buff, 0, len);
//					}
//					bufEcrivain.flush();
//					bufEcrivain.close();
//					VideoReader.close();
//				}
//			} catch (Exception e) {
//				LogHelper.i(TAG, "从raw复制图片失败！");
//				e.printStackTrace();
//			}
//		}
//		LogHelper.i(TAG, "从raw复制图片成功！");
//	}
	
	/*
	 * 读取raw里的config文件
	 */
//	public static void readConfig(Context context) {
//		Field[] raw = R.raw.class.getFields();
//		for (Field r : raw) {
//			try {
//				int id = context.getResources().getIdentifier(r.getName(), "raw",context.getPackageName());
//				if (r.getName().equals("fyconfig")) {
//					InputStream input = context.getResources().openRawResource(id);
//					 String jsonStr = new String(FileUtil.readInputStream(input));
////					 String password = "9588028820109132570743325311898426347857298773549468758875018579537757772163084478873699447306034466200616411960574122434059469100235892702736860872901247123456";
////					 System.out.println("------------");
//				     byte[] result = DES.encrypt(jsonStr.getBytes(),password);
////				     System.out.println("加密后："+new String(result));
////				     //直接将如上内容解密
////				     try {
////				             System.out.println("解密后："+new String(decryResult));
////				     } catch (Exception e1) {
////				             e1.printStackTrace();
////				     }
//					
//					JSONObject jsonObj = new JSONObject(jsonStr);
//					ConfigMessage.getInstance().jsonParse(jsonObj);
//				}
//			}catch(Exception e){
//				e.printStackTrace();
//			}
//		}
//	}

	/**
	 * 从输入流获取数据
	 * 
	 * @param input
	 * @return
	 * @throws IOException
	 */
	public static byte[] readInputStream(InputStream input) {
		ByteArrayOutputStream out = new ByteArrayOutputStream();
		//
		int length = 0;
		byte[] data = null;
		byte[] buffer = new byte[1024];
		try {
			while ((length = input.read(buffer)) != -1) {
				out.write(buffer, 0, length);
			}
			data = out.toByteArray();
			out.flush();
		} catch (Exception e) {
			e.printStackTrace();
			if (out != null) {
				try {
					out.close();
					return null;
				} catch (IOException e1) {
					e1.printStackTrace();
					return null;
				}
			}
		}
		return data;
	}

	/**
	 * SD卡是否可用
	 * 
	 * @return
	 */
	public static boolean isSDCardAvailable() {
		return Environment.getExternalStorageState().equals(
				Environment.MEDIA_MOUNTED);
	}

	/**
	 * 获取文件的输入流
	 * 
	 * @param path
	 * @return
	 */
	public static FileInputStream getFileInputStream(String path) {
		FileInputStream fis = null;
		try {
			File file = new File(path);
			if (file.exists()) {
				fis = new FileInputStream(file);
			}
		} catch (Exception e) {
			e.printStackTrace();
			if (fis != null) {
				try {
					fis.close();
					fis = null;
					return null;
				} catch (IOException e1) {
					e1.printStackTrace();
					return null;
				}
			}
		}
		return fis;
	}

	/**
	 * 获取文件的输出流
	 * 
	 * @param path
	 * @return
	 */
	public static OutputStream getFileOutputStream(String path) {
		FileOutputStream fos = null;
		try {
			File file = new File(path);
			if (file.exists()) {
				fos = new FileOutputStream(file);
			}
		} catch (Exception e) {
			if (fos != null) {
				try {
					fos.close();
					fos = null;
					return null;
				} catch (IOException e1) {
					e1.printStackTrace();
					return null;
				}
			}
		}
		return fos;
	}

	/**
	 * 获取文件的数据
	 * 
	 * @param path
	 * @return
	 */
	public static byte[] getFileData(String path) {
		byte[] data = null;// 返回的数据
		FileInputStream fis = null;
		try {
			File file = new File(path);
			if (file.exists()) {
				data = new byte[(int) file.length()];
				fis = new FileInputStream(file);
				fis.read(data);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (fis != null) {
				try {
					fis.close();
					fis = null;
				} catch (IOException e) {
					e.printStackTrace();
					return null;
				}
			}
		}
		return data;
	}

	/**
	 * 文件大小
	 * 
	 * @param path
	 * @return
	 */
	public static int getFileSize(String path) {
		return (int) new File(path).length();
	}

	/**
	 * 重写文件的数据
	 * 
	 * @param path
	 * @param data
	 */
	public static void rewriteData(String path, byte[] data) {
		FileOutputStream fos = null;
		try {
			File file = new File(path);
			if (file.exists()) {
				fos = new FileOutputStream(file, false);
				fos.write(data);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (fos != null) {
				try {
					fos.flush();
					fos.close();
					fos = null;
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}

	/**
	 * 重写文件的数据
	 * 
	 * @param path
	 * @param is
	 */
	public static void rewriteData(String path, InputStream is) {
		FileOutputStream fos = null;
		try {
			File file = new File(path + "aaa");
			if (file.exists()) {
				fos = new FileOutputStream(file, false);
				byte[] data = new byte[1024];
				int receive = 0;
				while ((receive = is.read(data)) != -1) {
					fos.write(data, 0, receive);
					fos.flush();
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (fos != null) {
				try {
					fos.close();
					fos = null;
				} catch (IOException e) {
					e.printStackTrace();
				}
				if (is != null) {
					try {
						is.close();
						is = null;
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
			}
		}
	}

	/**
	 * 向文件的末尾添加数据
	 * 
	 * @param path
	 * @param data
	 */
	public static boolean appendData(String path, byte[] data) {
		FileOutputStream fos = null;
		try {
			File file = new File(path);
			if (file.exists()) {
				fos = new FileOutputStream(file, true);
				fos.write(data);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (fos != null) {
				try {
					fos.flush();
					fos.close();
					fos = null;
				} catch (IOException e) {
					e.printStackTrace();
					return false;
				}
			}
		}
		return true;
	}

	/**
	 * 向文件末尾添加数据
	 * 
	 * @param path
	 * @param is
	 */
	public static void appendData(String path, InputStream is) {
		FileOutputStream fos = null;
		try {
			File file = new File(path);
			if (file.exists()) {
				fos = new FileOutputStream(file, true);
				byte[] data = new byte[1024];
				int receive = 0;
				while ((receive = is.read(data)) != -1) {
					fos.write(data, 0, receive);
					fos.flush();
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (fos != null) {
				try {
					fos.close();
					fos = null;
				} catch (IOException e) {
					e.printStackTrace();
				}
				if (is != null) {
					try {
						is.close();
						is = null;
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
			}
		}
	}

	/**
	 * 删除文件或文件夹(包括目录下的文件)
	 * 
	 * @param path
	 */
	public static void deleteFile(String filePath) {
		if (TextUtils.isEmpty(filePath)) {
			return;
		}
		try {
			File f = new File(filePath);
			if (f.exists() && f.isDirectory()) {
				File[] delFiles = f.listFiles();
				if (delFiles != null) {
					for (int i = 0; i < delFiles.length; i++) {
						deleteFile(delFiles[i].getAbsolutePath());
					}
				}
			}
			f.delete();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 删除文件
	 * 
	 * @param filePath
	 * @param deleteParent
	 *            是否删除父目录
	 */
	public static void deleteFile(String filePath, boolean deleteParent) {
		if (filePath == null) {
			return;
		}
		try {
			File f = new File(filePath);
			if (f.exists() && f.isDirectory()) {
				File[] delFiles = f.listFiles();
				if (delFiles != null) {
					for (int i = 0; i < delFiles.length; i++) {
						deleteFile(delFiles[i].getAbsolutePath(), deleteParent);
					}
				}
			}
			if (deleteParent) {
				f.delete();
			} else if (f.isFile()) {
				f.delete();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 创建一个空的文件(创建文件的模式，已经存在的是否要覆盖)
	 * 
	 * @param path
	 * @param mode
	 */
	public static boolean createFile(String path, int mode) {
		if (TextUtils.isEmpty(path)) {
			return false;
		}
		try {
			File file = new File(path);
			if (file.exists()) {
				if (mode == FileUtil.MODE_COVER) {
					file.delete();
					file.createNewFile();
				}
			} else {
				// 如果路径不存在，先创建路径
				File mFile = file.getParentFile();
				if (!mFile.exists()) {
					mFile.mkdirs();
				}
				file.createNewFile();
			}
		} catch (Exception e) {
			return false;
		}
		return true;
	}

	/**
	 * 创建一个空的文件夹(创建文件夹的模式，已经存在的是否要覆盖)
	 * 
	 * @param path
	 * @param mode
	 */
	public static boolean createFolder(String path, int mode) {
		try {
			// LogUtil.debug(path);
			File file = new File(path);
			if (file.exists()) {
				if (mode == FileUtil.MODE_COVER) {
					file.delete();
					file.mkdirs();
				}
			} else {
				file.mkdirs();
			}
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}

	/**
	 * 获取文件大小
	 * 
	 * @param path
	 * @return
	 */
	public static long getSize(String path) {
		if (TextUtils.isEmpty(path)) {
			return 0;
		}
		long size = 0;
		try {
			File file = new File(path);
			if (file.exists()) {
				size = file.length();
			}
		} catch (Exception e) {
			return 0;
		}
		return size;
	}

	/**
	 * 判断文件或文件夹是否存在
	 * 
	 * @param path
	 * @return true 文件存在
	 */
	public static boolean isExist(String path) {
		if (TextUtils.isEmpty(path)) {
			return false;
		}
		boolean exist = false;
		try {
			File file = new File(path);
			exist = file.exists();
		} catch (Exception e) {
			return false;
		}
		return exist;
	}

	/**
	 * 重命名文件/文件夹
	 * 
	 * @param path
	 * @param newName
	 */
	public static boolean rename(final String path, final String newName) {
		boolean result = false;
		if (TextUtils.isEmpty(path) || TextUtils.isEmpty(newName)) {
			return result;
		}
		try {
			File file = new File(path);
			if (file.exists()) {
				result = file.renameTo(new File(newName));
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		return result;
	}

	/**
	 * 列出目录文件
	 * 
	 * @return
	 */
	public static File[] listFiles(String filePath) {
		File file = new File(filePath);
		if (file.exists() && file.isDirectory()) {
			return file.listFiles();
		}
		return null;
	}

	/**
	 * 移动文件
	 * 
	 * @param oldFilePath
	 *            旧路径
	 * @param newFilePath
	 *            新路径
	 * @return
	 */
	public static boolean moveFile(String oldFilePath, String newFilePath) {
		if (TextUtils.isEmpty(oldFilePath) || TextUtils.isEmpty(newFilePath)) {
			return false;
		}
		File oldFile = new File(oldFilePath);
		if (oldFile.isDirectory() || !oldFile.exists()) {
			return false;
		}
		try {
			File newFile = new File(newFilePath);
			if (!newFile.exists()) {
				newFile.createNewFile();
			}
			BufferedInputStream bis = new BufferedInputStream(
					new FileInputStream(oldFile));
			FileOutputStream fos = new FileOutputStream(newFile);
			byte[] buf = new byte[1024];
			int read;
			while ((read = bis.read(buf)) != -1) {
				fos.write(buf, 0, read);
			}
			fos.flush();
			fos.close();
			bis.close();
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}

}
